<?php
namespace Tbf\Bundle\DatabaseBundle;
use Tbf\Bundle\DatabaseBundle\Exception\DatabaseException;
/**
 * 数据库配置类,提供各种数据库配置的接口之间的转换
 * 基本信息
 * host        '127.0.0.1'
 * username  'root'
 * password  '12123'
 * dbname    'xxx_xxx'
 * prefix    'xxx_'
 * 可选信息
 * type      'pdo_mysql'
 * encode     'utf8'
 * persistent  false
 * port     3306
 * */
class DbConfig
{
    const MYSQL = 'mysql';
    const SQLITE = 'sqlite';
    protected $config = array();
    public static $tran_table = array(
        'cakephp'=>
            array('datasource'=>
                array('mysql'=>'Database/Mysql')
            )
    );
    //构造用方法
    public function copy()
    {
        return clone $this;
    }
    public static function fromTbf($config)
    {
        return new static($config);
    }
    public function __construct($db_config)
    {
        $this->config = $db_config;
    }

    //基本信息
    public function getHost()
    {
        if (empty($this->config['host'])) {
            return '127.0.0.1';
        }

        return $this->config['host'];
    }
    public function setHost($host)
    {
        $this->config['host'] = $host;

        return $this;
    }
    public function getUsername()
    {
        if (empty($this->config['username'])) {
            return 'root';
        }

        return $this->config['username'];
    }
    public function setUsername($username)
    {
        $this->config['username'] = $username;

        return $this;
    }
    public function getPassword()
    {
        if (empty($this->config['password'])) {
            throw new DatabaseException('need config database password');
        }

        return $this->config['password'];
    }
    public function setPassword($password)
    {
        $this->config['password'] = $password;

        return $this;
    }
    public function getDbName()
    {
        if (empty($this->config['dbname'])) {
            throw new DatabaseException('need config database dbname');
        }

        return $this->config['dbname'];
    }
    public function setDbName($dbname)
    {
        $this->config['dbname'] = $dbname;

        return $this;
    }
    public function getPrefix()
    {
        if (empty($this->config['prefix'])) {
            return '';
        }

        return $this->config['prefix'];
    }
    public function setPrefix($prefix)
    {
        $this->config['prefix'] = $prefix;

        return $this;
    }

    //可选信息
    public function getType()
    {
        if (empty($this->config['type'])) {
            return 'mysql';
        }

        return $this->config['type'];
    }
    public function setType($type)
    {
        $this->config['type'] = $type;

        return $this;
    }
    public function getEncode()
    {
        if (empty($this->config['encode'])) {
            return 'utf8';
        }

        return $this->config['encode'];
    }
    public function setEncode($encode)
    {
        $this->config['encode'] = $encode;

        return $this;
    }
    public function getPersistent()
    {
        if (empty($this->config['persistent'])) {
            return false;
        }

        return $this->config['persistent'];
    }
    public function setPersistent($persistent)
    {
        $this->config['persistent'] = $persistent;

        return $this;
    }
    public function setPort($port)
    {
        $this->config['port'] = $port;

        return $this;
    }
    public function getPort()
    {
        if (empty($this->config['port'])) {
            return '3306';
        }

        return $this->config['port'];
    }

    //数据库接口转换
    public function toTbf()
    {
        return array(
            'type'=>$this->getType(),
            'host'=>$this->getHost(),
            'username'=>$this->getUsername(),
            'password'=>$this->getPassword(),
            'dbname'=>$this->getDbname(),
            'prefix'=>$this->getPrefix(),
            'encode'=>$this->getEncode(),
            'persistent'=>$this->getPersistent(),
            'port'=>$this->getPort()
        );
    }

    public function toCakephp()
    {
        //$type = $this->getType();
        //$type = self::$tran_table['cakephp']['datasource'][$type];
        return array(
            'datasource'=>'Database/Mysql',
            'host'=>$this->getHost(),
            'login'=>$this->getUsername(),
            'password'=>$this->getPassword(),
            'database'=>$this->getDbname(),
            'prefix'=>$this->getPrefix(),
            'encoding'=>$this->getEncode(),
            'persistent'=>$this->getPersistent()
        );
    }
    public function toPhpcms()
    {
        $pconnect = $this->getPersistent();
        if ($pconnect) {
            $pconnect = 1;
        } else {
            $pconnect = 0;
        }

        return array(
        'hostname' => $this->getHost(),
        'database' => $this->getDbname(),
        'username' => $this->getUsername(),
        'password' => $this->getPassword(),
        'tablepre' => $this->getPrefix(),
        'charset' => $this->getEncode(),
        'type' => $this->getType(),
        'debug' => true,
        'pconnect' => $pconnect,
        'autoconnect' => 0
        );
    }
    public function toPhpwind()
    {
        return array(
        'dbhost'=>$this->getHost(),
        'dbuser'=>$this->getUserName(),
        'dbpw'=>$this->getPassword(),
        'dbname'=>$this->getDbname(),
        'database'=>'mysqli',
        'PW'=>$this->getPrefix(),
        'pconnect'=>'0',
        'charset'=>$this->getEncode(),
        );
    }
    public function toUcenter()
    {
        $prefix = '`'.$this->getDbname().'`.'.$this->getPrefix();

        return array(
            'UC_CONNECT'=>'mysql',
            'UC_DBHOST'=>$this->getHost(),
            'UC_DBUSER'=>$this->getUsername(),
            'UC_DBPW'=>$this->getPassword(),
            'UC_DBNAME'=>$this->getDbname(),
            'UC_DBCHARSET'=>$this->getEncode(),
            'UC_DBTABLEPRE'=>$prefix,
            'UC_DBCONNECT'=>0
        );
    }
    /**
//db
$_CONF['db']['host']        = "localhost";          //数据库主机
$_CONF['db']['port']        = "3306";           //数据库端口
$_CONF['db']['user']        = "root";           //数据库用户名
$_CONF['db']['pass']        = "xxxpass";         //数据库密码
$_CONF['db']['dbname']      = "fruit_v2_dev";           //数据库名
$_CONF['db']['dbprefix']    = "cart_";      //数据库表前缀
$_CONF['db']['driver']      = "pdo";        //数据库驱动 PDO或mysql
$_CONF['db']['dbcharset']   = "utf8";   //数据库编码
     * */
    public function toYuncart()
    {
        return array(
            'host'=>$this->getHost(),
            'port'=>$this->getPort(),
            'user'=>$this->getUsername(),
            'pass'=>$this->getPassword(),
            'dbname'=>$this->getDbName(),
            'dbprefix'=>$this->getPrefix(),
            'driver'=>'pdo',
            'dbcharset'=>$this->getEncode(),
        );
    }
    /*
     *
array(
    'dsn' => 'mysql:host=localhost;dbname=fruit_v2_dev;port=3306',
    'user' => 'root',
    'pwd' => 'xxxpass',
    'charset' => 'utf8',
    'tableprefix' => 'pw_',
    'engine' => 'MyISAM',
);

     * */
    public function toPhpwind9()
    {
        $dsn = 'mysql:host='.$this->getHost().
        ';dbname='.$this->getDbName().
        ';port='.$this->getPort();
        $ret = array(
            'dsn' => $dsn,
            'user'=>$this->getUsername(),
            'pwd'=>$this->getPassword(),
            'charset'=>$this->getEncode(),
            'tableprefix'=>$this->getPrefix(),
            'engine'=>'MyISAM',
        );

        return $ret;
    }
}
